<?php
namespace Wx\Controller;
use Think\Controller;
/*
*检测用户是否关注，将其状态存入session
*
*/
class AuthnoticeController extends Controller {
    // 初始化方法-创建微信菜单
    public function _initialize(){
		$token=$this->get_access_token();
		$str='
{
    "button": [
        {
            "type": "view", 
            "name": "我的社区", 
            "url": "http://www.xiangtws.com/Wx/"
        }, 
        {
            "type": "view", 
            "name": "最美乡村", 
            "url": "http://news.xiangtws.com/Wx/Village/country/"
        }, 
        {
            "name": "加入我们", 
            "sub_button": [
                {
                    "type": "view", 
                    "name": "个人中心", 
                    "url": "http://www.xiangtws.com/Wx/User/index.html"
                }, 
                {
                    "type": "view", 
                    "name": "加入我们", 
                    "url": "http://www.xiangtws.com/Wx/Joinus/join/"
                }
            ]
        }
    ]
}		
		';
		$url='https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$token;
		$result=$this->postcurl($url,$str);
	}//end
	//请求接口方法
	function postcurl($url,$data = null){		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		if (!empty($data)){
			curl_setopt($ch, CURLOPT_POST, TRUE);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		}
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$output = curl_exec($ch);
		curl_close($ch);
		return 	$output=json_decode($output,true);			
	}
	public function get_access_token(){
		/*
		//把access_token存入缓存，一个小时换一次
		$cache=S(array(
			'type'=>'memcache',
			'host'=>'127.0.0.1',
			'port'=>'11211',
			'prefix'=>'token',
			'expire'=>86400)
		);
		$token = '';
		$oldtime=0;
		if($cache->oldtime>0)$oldtime=$cache->oldtime;//该缓存数据时间
		$nowtime=time();//当前时间
		if($nowtime-$oldtime>3600){
			//如果大于一个小时，获取新的token
			$access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".C('WX_API.appid')."&secret=".C('WX_API.secret');
			$access_msg = json_decode(file_get_contents($access_token));
			$token = $access_msg->access_token;//获取最新token
			//存储token
			$cache->oldtime=time();
			$cache->token=$token;
		}else{
			$token = $cache->token;//使用老的token 
		}
		dump($token);*/
        // access_token 应该全局存储与更新，以下代码以写入到文件中做示例
        $data = json_decode(file_get_contents("access_token.json"));
        if ($data->expire_time < time()) {
			
            /*$url = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".C('WX_API.appid')."&secret=".C('WX_API.secret'));
            $res = json_decode($url,true);
            $access_token = $res->access_token;*/
			$access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".C('WX_API.appid')."&secret=".C('WX_API.secret');
			$access_msg = json_decode(file_get_contents($access_token));
			$access_token = $access_msg->access_token;//获取最新token
			//dump($access_msg);
            if ($access_token) {
                $data->expire_time = time() + 300;
                $data->access_token = $access_token;
                $fp = fopen("access_token.json", "w");
                fwrite($fp, json_encode($data));
                fclose($fp);
            }
        } else {
            $access_token = $data->access_token;
        }
		//dump($access_token);
		return $access_token;
	}//end fun
	//检测是否关注
    public function checkFocus(){
		$token=$this->get_access_token();
		//dump($token);
		$subscribe_msg = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$token."&openid=".session('openid');
		$subscribe = json_decode(file_get_contents($subscribe_msg));
		//dump($subscribe);
		/*$subscribe打印出来的信息 
		object(stdClass)#7 (16) {
		  ["subscribe"] => int(1)
		  ["openid"] => string(28) "o2pfr0vXjjsXbv07IvySpz4Gi7ZE"
		  ["nickname"] => string(9) "蒋杰夫"
		  ["sex"] => int(1)
		  ["language"] => string(5) "zh_CN"
		  ["city"] => string(6) "武汉"
		  ["province"] => string(6) "湖北"
		  ["country"] => string(6) "中国"
		  ["headimgurl"] => string(134) "http://thirdwx.qlogo.cn/mmopen/jAO8YFFcSI0tSzPREb1rsibEfNhibltdjjUMykjO7VUr1carhT4vSSxTMjQhfCe2bj9pzxN06r6yGYFCC1ibVdV8cOMC0yw1B6J/132"
		  ["subscribe_time"] => int(1527493307)
		  ["remark"] => string(0) ""
		  ["groupid"] => int(0)
		  ["tagid_list"] => array(0) {
		  }
		  ["subscribe_scene"] => string(16) "ADD_SCENE_SEARCH"
		  ["qr_scene"] => int(0)
		  ["qr_scene_str"] => string(0) ""
		}
		*/
		$gzxx = $subscribe->subscribe;
		//
		$member=D('member');
		if($gzxx === 1){
			//echo "已关注";
			$member-> where('id='.session('u_id'))->setField('focus',1);
			session('focus',1);  //"已关注"
		}else{
			//echo "未关注";
			$member-> where('id='.session('u_id'))->setField('focus',0);
			session('focus',0);  //"未关注"
		}
		
	}//end fun
	
	//关注推送消息时候用于检测是否关注
    public function checkFocusMsg($u_id=0){
		$member=D('member');
		if(empty($u_id) || $u_id=='' || $u_id==null || $u_id==0){
			return 0;
		}else{
			$info=$member->where("id=".$u_id)->find();
		}//end if
		
		$token=$this->get_access_token();
		
		$subscribe_msg = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$token."&openid=".$info['openid'];
		$subscribe = json_decode(file_get_contents($subscribe_msg));
		dump($subscribe);

		$gzxx = $subscribe->subscribe;
		//
		if($gzxx === 1){
			//echo "已关注";
			$member-> where('id='.$u_id)->setField('focus',1);
			session('focus'.$u_id,1);  //"已关注"
		}else{
			//echo "未关注";
			$member-> where('id='.$u_id)->setField('focus',0);
			session('focus'.$u_id,0);  //"未关注"
		}
		
	}//end fun
}//end class